package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.MinIoTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.thread.WmUserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
	@Autowired
	private MinIoTemplate minIoTemplate;
	
	@Autowired
	private WmMaterialMapper wmMaterialMapper;
	
	@Override
	public PageResponseResult list(WmMaterialDto dto) {
		//如果参数为null直接返回0页0条0总数
		if (dto == null) {
			return new PageResponseResult(0,0,0);
		}

		//组装查询条数
		LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(dto.getIsCollection() == 1,WmMaterial::getIsCollection,dto.getIsCollection());
		Page<WmMaterial> page = new Page<>(dto.getPage(),dto.getSize());
		Page<WmMaterial> pageResult = wmMaterialMapper.selectPage(page, wrapper);
		//创建并组装返回值对象
		PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) pageResult.getTotal());
		pageResponseResult.setData(pageResult.getRecords());
		//返回给前端
		return pageResponseResult;
	}
	
	@Override
	public ResponseResult uploadPicture(MultipartFile multipartFile) throws IOException {
		//获取上传图片的输入流
		InputStream inputStream = multipartFile.getInputStream();
		
		//把图片上传到minio
		String contentType = multipartFile.getContentType();
		//图片后缀提取
		if (StringUtils.isEmpty(contentType)) {
			return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
		}
		
		String[] split = contentType.split("/");
		String stringBuffer = split[1];
		
		String path = minIoTemplate.uploadFile("", System.currentTimeMillis() + stringBuffer, inputStream, "image");
		

		//将上传后的url写入数据库
		WmMaterial material = new WmMaterial();
		material.setType((short)0);
		material.setCreatedTime(new Date());
		material.setIsCollection((short)0);
		material.setUrl(path);
		material.setUserId(WmUserThreadLocal.get().getId());
		int insert = wmMaterialMapper.insert(material);
		if (insert < 1) {
			return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
		}
		
		//将创建好的图片信息返回给前端
		return ResponseResult.okResult(material);
	}
}
